package com.fcat.common.user.entity;

import com.alibaba.fastjson.JSONObject;
import lombok.Builder;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

@Data
@Builder(toBuilder = true)
@Table(name = "sys_certificate")
public class Certificate {
    /**
     * 自增id
     */
    @Id
    private Long id;

    private Long userId;

    /**
     * 名称
     */
    private String name;

    /**
     * 资格证书编码
     */
    @Column(name = "`code`")
    private String code;

    /**
     * 获证时间
     */
    @Column(name = "certified_time")
    private Date certifiedTime;

    /**
     * 证书注册类型
     */
    @Column(name = "reg_type")
    private String regType;

    /**
     * 证书附件id
     */
    @Column(name = "file_id")
    private String fileId;

    /**
     * 证书有效时间
     */
    @Column(name = "validity_time")
    private Date validityTime;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private Date createTime;

    /**
     * 更新时间
     */
    @Column(name = "update_time")
    private Date updateTime;

    public Certificate() {
    }

    public Certificate(Long id, Long userId, String name, String code, Date certifiedTime, String regType, String fileId, Date validityTime, Date createTime, Date updateTime) {
        this.id = id;
        this.userId = userId;
        this.name = name;
        this.code = code;
        this.certifiedTime = certifiedTime;
        this.regType = regType;
        this.fileId = fileId;
        this.validityTime = validityTime;
        this.createTime = createTime;
        this.updateTime = updateTime;
    }

    public Certificate(JSONObject jsonObject) {
        this.userId = jsonObject.getLong("userId");
        this.name = jsonObject.getString("certifiedName");
        this.code = jsonObject.getString("certifiedName");
        this.certifiedTime = jsonObject.getLong("certifiedTime") == null ? null : new Date(jsonObject.getLong("certifiedTime"));
        this.regType = jsonObject.getString("certificateRegType");
        this.fileId = jsonObject.getString("fileId");
        this.validityTime = jsonObject.getLong("certificateValidityTime") == null ? null : new Date(jsonObject.getLong("certificateValidityTime"));
    }
}